package com.zimin.cloud.template;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 自定义模板引擎
 *
 * @author zimin
 * @since 2021-10-16 20:33
 */
public class CustomerTemplateEngine extends FreemarkerTemplateEngine {

    @Override
    protected void outputCustomFile(Map<String, String> customFile, TableInfo tableInfo, Map<String, Object> objectMap) {
        List<String> packages = Arrays.asList("BO","VO");
        customFile.forEach((key, value) -> {
            String entityName = tableInfo.getEntityName();
            String[] split = key.split("\\.");
            String keyPrefix = split[0].replace("I%s","").replace("%s", "");
            keyPrefix = packages.contains(keyPrefix) ? keyPrefix : StringUtils.firstToLowerCase(keyPrefix);
            String otherPath = getPathInfo(OutputFile.other);
            String fileName = String.format((otherPath + File.separator + "%s" + File.separator + key),
                    keyPrefix, entityName);
            outputFile(new File(fileName), objectMap, value);
        });
    }

}
